#include <stdio.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "modbus.h"
#include "timer.h"

static bool modbus_command(uint8_t function, uint8_t *data, uint8_t length, bool broadcast)
{
    switch (function) {
        case MODBUS_READ_DISCRETE_INPUTS:
        case MODBUS_READ_COILS:
        case MODBUS_WRITE_SINGLE_COIL:
        case MODBUS_WRITE_MULTIPLE_COILS:
            asm("nop");
            return true;

        case MODBUS_READ_INPUT_REGISTER:
        case MODBUS_READ_HOLDING_REGISTER:
        case MODBUS_WRITE_REGISTER:
        case MODBUS_WRITE_MULTIPLE_REGISTERS:
        case MODBUS_READWRITE_MULTIPLE_REGISTERS:
            asm("nop");
            return true;

        case MODBUS_READ_FILE_RECORD:
        case MODBUS_WRITE_FILE_RECORD:
            asm("nop");
            return true;

        default:
            return false;
    }
}


ISR(TIMER2_COMP_vect, ISR_BLOCK)
{
    TickCounter++;
}


int main(void)
{
    // SysTick init
    TCCR2 = _BV(CS21) | _BV(CS20) | _BV(WGM21);
    OCR2 = 249;
    TIMSK |= _BV(OCIE2);

    // ModBus init
    modbus_init(0x34, &modbus_command);
    sei();
    
    while (1) {
        asm("nop");
        modbus_task();
    }
}
